<?php
namespace app\mini\model;

use think\Model;
use traits\model\SoftDelete;

class Recharge extends Model {
    
//    protected $autoWriteTimestamp = true;

//    use SoftDelete;
//
//    protected $deleteTime = 'delete_time';

    public function Team() {
        return $this->hasOne('team', 'id', 'initiator')->field('team_name');
    }

    /*
     * 查询企业上传项目信息
     * @param $where where
     * @param $field field
     * @author cpy(1442435897@qq.com)
     */
    public function getInfo($where,$field = '*') {
        $res = $this->field($field)->where($where)->find();
        return empty($res)?array():$res->toArray();
    }
    
    /*
     * 查询所有企业用户项目
     * @param $where where
     * @param $field field
     * @author cpy(1442435897@qq.com)
     */
    public function selectInfo($where,$field = '*') {
        $res = $this->field($field)->where($where)->select();
        return empty($res)?array():$res->toArray();
    }
    
    /*
     * 查询所有企业用户项目
     * @param $where where
     * @param $field field
     * @author cpy(1442435897@qq.com)
     */
    public function selectGroupInfo($where,$group,$field = '*') {
        $res = $this->field($field)->where($where)->group($group)->select();
        return empty($res)?array():$res->toArray();
    }
    
    /*
     * 联合查询所有企业用户项目
     * @param $where where
     * @param $field field
     * @author cpy(1442435897@qq.com)
     */
    public function selectJoinGroupInfo($where,$group,$field = '*') {
        $res = $this->alias('a')->join('member b','b.id=a.uid')->field($field)->where($where)->group($group)->select();
        return empty($res)?array():$res->toArray();
    }
    
    /*
     * 查询所有企业用户项目
     * @param $where where
     * @param $field field
     * @author cpy(1442435897@qq.com)
     */
    public function selectLimitInfo($where,$limit,$field = '*') {
        $res = $this->field($field)->where($where)->limit($limit)->select();
        return empty($res)?array():$res->toArray();
    }

    /**
     * 获取充值明细
     * @author hey_heyang(635870453@qq.com)
     */
    public function  getRechargeList($time_begin,$time_end){

        if($time_begin && $time_end){
            $time1 = date('Y-m-d H:i:s',strtotime($time_begin));
            $time2 = date('Y-m-d H:i:s',strtotime($time_end.'+ 1 day')-1);
            $where['r.date'] = array('between',$time1.','.date($time2));
        }elseif($time_begin){
            $where['r.date'] = array('>=',date('Y-m-d H:i:s',strtotime($time_begin)));
        }elseif($time_end){
            $where['r.date'] = array('<=',date('Y-m-d H:i:s',strtotime($time_end.'+ 1 day')-1));
        }

        $where['r.status'] = ['eq',2];
        $filed='r.money,r.date,m.username,m.mobile';
        $res=$this->alias('r')
            ->join('tf_member m','r.user_id=m.id')
            ->where($where)
            ->field($filed)
            ->limit(1000)
            ->order('r.id desc')
            ->select();
        $res= empty($res)?array():$res->toArray();
        $total= $this->alias('r')->where($where)->sum('money');
        $data['res'] = $res;
        $data['total'] = $total;
        return $data;
    }

}
